package commandExecution;


public class CommandGenerator {

	public static String generateFind(String path, String[] fileTypes) {
    	
		String command = "find "+ path + " \\(";
    	
    	for (int i = 0; i < fileTypes.length; i++) {
			command += " -iname *." + fileTypes[i] + " -print";
			if(i<fileTypes.length-1) {
				command += " -or";
			}
		}
    	
    	command += " \\)";
    	
		return command;
	}

	public static String generateCopy(String[] originPaths, String targetPath) {
		
		String command = "cp ";
		
		for (int i = 0; i < originPaths.length; i++) {
			command += escapePath(originPaths[i]) + " ";
		}
		
		command += targetPath;
		
		return command;
	}

	private static String escapePath(String path) {
		
		String[] temp = path.split(" ");
		
		if(temp.length<2) {
			return path;
		}
		
		String escaped = temp[0];
		
		for(int i=1; i<temp.length; i++) {
			escaped += "\\ " + temp[i];
		}
		
		return escaped;
	}
	
	public static String generate_mkdir(String targetPath, String dirName) {
		
		String command = "mkdir ";
		
		command += escapePath(targetPath) + "/" + escapePath(dirName);
		
		return command;
		
	}
	
	public static String generate_mv(String originPath, String targetPath) {
		
		String command = "mv ";
		
		command += escapePath(originPath) + " " + escapePath(targetPath);
		
		return command;
		
	}
	
}